﻿#include "svgcanvas.h"
#include "svgshape.h"

#include <gtest/gtest.h>

class SvgCanvasTest: public testing::Test
{
protected:
	void SetUp() override
	{
		canvas.ReSize(1000, 1000);

		auto line = std::make_unique<SvgLine>(QLineF{ {111, 222}, {333, 444} });
		line1 = line.get();
		canvas.AddShape(std::move(line));
		line = std::make_unique<SvgLine>(QLineF{ {11, 222}, {333, 444} });
		line2 = line.get();
		canvas.AddShape(std::move(line));
		line = std::make_unique<SvgLine>(QLineF{ {1, 222}, {333, 444} });
		line3 = line.get();
		canvas.AddShape(std::move(line));

		auto rect = std::make_unique<SvgRect>(QRectF{ QPointF{111, 222}, QSizeF{222, 222} });
		rect1 = rect.get();
		canvas.AddShape(std::move(rect));
	}

	SvgLine* line1 = nullptr;
	SvgLine* line2 = nullptr;
	SvgLine* line3 = nullptr;
	SvgRect* rect1 = nullptr;

	SvgCanvas canvas;
};

TEST_F(SvgCanvasTest, test_get)
{
	EXPECT_EQ(canvas.GetLastShape(), rect1);

	EXPECT_EQ(canvas.GetShapeByCoordinate({ 111, 223 }), rect1);
	EXPECT_EQ(canvas.GetShapeByCoordinate({ 0, 0 }), nullptr);
	EXPECT_EQ(canvas.GetShapeByCoordinate({ 11, 222 }), line2);

	EXPECT_EQ(std::vector<SvgShape*>({line2, line3}), canvas.GetShapeByRect({ 0, 220, 20, 20 }));
}

TEST_F(SvgCanvasTest, test_change)
{
	canvas.DeleteShape(rect1);
	EXPECT_EQ(canvas.GetLastShape(), line3);

	// 注意，界面显示的前后和数组内的前后是相反的
	canvas.BringShapesToFront({line1, line2});
	EXPECT_EQ(canvas.GetLastShape(), line2);

	canvas.SendShapesToBack({line2});
	EXPECT_EQ(canvas.GetLastShape(), line1);
}
